<template>
  <div class="InterviewSituation">
    <!--卡片视图·-->
    <el-card class="box-card">
      <el-row :gutter="20">
        <el-col :span="10">
          <el-input
            placeholder="请输入查询条件（暂仅支持查询培训主题）"
            v-model="queryInfo.keyWord"
            clearable
            @clear="getRecordList"
            @change="getRecordList"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getRecordList"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="1">
          <el-tooltip
            class="item"
            effect="dark"
            content="刷新记录"
            placement="top"
          >
            <el-button
              circle
              type="primary"
              size="medium"
              :icon="isLoading.status ? 'el-icon-loading' : 'el-icon-refresh'"
              :disabled="isLoading.status ? true : false"
              @click="getRecordList"
            >
            </el-button>
          </el-tooltip>
        </el-col>
        <!-- <el-col v-if="!isGuest" :span="2" :offset="offset">
          <el-button
            type="primary"
            @click="addRecord()"
            :loading="upload.status"
            >{{ upload.text }}</el-button
          >
        </el-col> -->
      </el-row>
      <!--培训记录显示详情区域-->
      <el-table
        :data="recordList"
        v-loading="isLoading.status"
        style="width: 100%"
        border
        stripe
      >
        <el-table-column
          type="index"
          label="#"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="subject"
          label="培训主题"
          width="200"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="groupName"
          label="培训组"
          width="200"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="lecturer"
          label="主讲人"
          min-width="90"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="isWithTea"
          label="指导老师参与"
          min-width="120"
          align="center"
          :formatter="teaFormatter"
        ></el-table-column>
        <el-table-column
          prop="trainingDate"
          label="培训时间"
          min-width="180"
          align="center"
          :formatter="trainingDateFormatter"
        ></el-table-column>
        <el-table-column
          prop="duration"
          label="培训时长（分钟）"
          min-width="140"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="trainingLocation"
          label="培训地点"
          min-width="100"
          align="center"
        ></el-table-column>
        <el-table-column fixed="right" label="操作" width="150" align="center">
          <template slot-scope="scope">
            <!--查看按钮-->
            <el-tooltip
              effect="dark"
              content="查看详情"
              placement="top"
              :enterable="false"
            >
              <el-button
                type="primary"
                icon="el-icon-s-data"
                plain
                size="mini"
                @click="singleRecord(scope.row)"
              >
              </el-button>
            </el-tooltip>
            <!--删除按钮-->
            <el-tooltip
              v-show="!isGuest"
              effect="dark"
              content="删除记录"
              placement="top"
              :enterable="false"
            >
              <el-button
                @click="removeRecordById(scope.row.recordId)"
                type="danger"
                icon="el-icon-delete"
                plain
                size="mini"
              >
              </el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <!--分页-->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pageNum"
        :page-sizes="[2, 5, 10, 30]"
        :page-size="queryInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="totalRecord"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import configList from "../config.js";
import dateUtils from "../utils/date.js";
export default {
  props: {
    groupId: Number,
    isGuest: Boolean,
  },

  data() {
    return {
      upload: {
        text: "点击上传培训记录",
        status: false,
      },
      queryInfo: {
        keyWord: "", //搜索关键字
        pageNum: 1, //页数
        pageSize: 10, //每页的size
      },
      addForm: {},
      totalRecord: 0,
      recordList: [],
      offset: 9,
      datetime: {
        date: "",
        time: "",
      },
      isLoading: {
        status: false,
      },
      productImgs: [],
      dialogVisible: false,
      dialogImageUrl: "",
      data: {
        isWithTea(is) {
          if (typeof is == "undefined") return "否";
          else if (is == 1) return "否";
          else return "是";
        },
      },
    };
  },

  created() {
    this.isWithTeaType = configList.isWithTeaType;
  },

  methods: {
    teaFormatter(row) {
      return row.isWithTeacher ? "是" : "否";
    },
    trainingDateFormatter(row) {
      return dateUtils.timestampToDateWithoutSecond(row.trainingDate);
    },
    /**
     * 根据id删除培训记录
     */
    removeRecordById(id) {
      // console.log("confirm");
      this.$confirm("此操作将永久删除该条记录, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(async () => {
        console.log("deleteid=", id);
        const res = await this.$axios.get(
          `${this.HOST}/record/api/deleteRecord/${id}`,
          {}
        );
        if (res.data.meta.code == 200) {
          this.$message({
            message: "删除成功",
            type: "success",
          });
        } else {
          this.$message({
            message: res.data.meta.msg,
            type: "success",
          });
        }
        this.getRecordList();
      });
    },
    async getRecordList() {
      this.isLoading.status = true;
      console.log("qu",this.queryInfo);
      const params = {
        pageNum: this.queryInfo.pageNum,
        pageSize: this.queryInfo.pageSize,
        isWithTeacher: this.queryInfo.teaType,
        startDate: this.queryInfo.startDate,
        endDate: this.queryInfo.endDate,
        duration: this.queryInfo.duration,
        groupId: parseInt(
          this.queryInfo.groupId[this.queryInfo.groupId.length - 1]
        ),
        keyword: this.queryInfo.keyWord,
      };
      // console.log("params", params);
      const res = await this.$axios.post(
        `${this.HOST}/record/api/recordList`,
        params
      );
      // console.log("res=", res);
      if (res.status == 200) {
        if (res.data.meta.code != 200) {
          this.$message({
            message: res.data.meta.msg,
            type: "error",
          });
          return;
        }
        // =========== 执行 ============
        var data = res.data.data.recordList;
        this.recordList = data.list;
        this.totalRecord = data.total;
        console.log("获取记录成功！", this.recordList);
      } else if (res.status != 200) {
        this.$message({
          message: "获取失败！",
          type: "error",
        });
      }
      this.isLoading.status = false;
    },
    singleRecord(row) {
      //  console.log("row",row);
      this.$router.push({
        name: "record",
        params: {
          recordId: row.recordId,
        },
      });
    },
    /**
     * 监听pagesiez的改变
     * newSiez 当前的每一页的size
     */
    handleSizeChange(newSize) {
      this.queryInfo.pageSize = newSize;
      this.getRecordList();
    },
    /**
     * 监听页码值变化
     * newPage 当前页
     */
    handleCurrentChange(newPage) {
      this.queryInfo.pageNum = newPage;
      this.getRecordList();
    },
    /**
     * 添加用户对话框关闭事件
     */
    settingDialogClosed(refName) {
      console.log(refName);
      // this.$refs[refName].resetFields();
    },
    handlePictureCardPreview(file) {
      //预览图片时调用
      console.log(file);
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    updateQueryInfo(info) {
      // console.log("uopdate", info);
      this.queryInfo = Object.assign(info, this.queryInfo);
      this.getRecordList();
    },
    showPic() {
      this.settingDialogVisible = !this.settingDialogVisible;
    },
  },
};
</script>

<style lang="less" scoped>
</style>
